<style>
#SystemCgiQueryTable{
	margin: 4px 1px;
}
#QueryCgiPathText{
	margin-right: 8px;
}
#QueryCgiSystemText{
	margin: 7px 2px 7px 24px;
}
#QueryCgiSystemLabel{
	bottom: 2px;
	position: relative;
}
#QueryCgiEnabledText{
	margin-right: 8px;
}
#QueryCgiConfigButton{
	margin-right: 8px;
}
</style>

<div>
	<table id='SystemCgiQueryTable'>
		<tr>
			<td>
				<v-text id='QueryCgiPathText' title='接口路径' placeholder='模糊匹配'></v-text>
				<v-select id='QueryCgiEnabledText' title='接口类型' option='全部|无效|私有|内部|公开'></v-select>
				<v-button id='QueryCgiConfigButton' title='查询'></v-button>
				<input id='QueryCgiSystemText' type='checkbox'/><span id='QueryCgiSystemLabel'>系统接口</span>
			</td>
		</tr>
	</table>
	<div id='SystemCgiListDiv'></div>
</div>

<script>
{
	let szlist = [0, 1, 2, 4, 8, 16, 32, 64];
	let cntlist = [0, 1, 2, 5, 10, 20, 50, 100, 200, 500];
	let hostcntlist = [0, 10, 20, 50, 100, 200, 500, 1000, 5000, 10000, 50000];

	getVue('SystemCgiQueryTable');

	let vmdata = {
		title: {
			path: '接口路径',
			enabled: centerText('接口类型', 'bold'),
			maxsz: centerText('请求大小', 'bold'),
			maxcnt: centerText('并发上线', 'bold'),
			hostmaxcnt: centerText('主机限制', 'bold'),
			daily: centerText('今日统计', 'bold'),
			meancost: centerText('耗时(微秒)', 'bold'),
			remark: '接口说明'
		},
		check: function(data){
			$.each(data.list, function(i, m){
				if (m.maxsz){
					m.maxsz = m.maxsz / 1024 / 1024;
					if (m.flag == 0){
						$.each(data.list, function(j, n){
							if (n.flag > 0 && n.path.indexOf(m.path) == 0){
								m.daily += n.daily;
							}
						});
					}
				}
			});
		},
		filter: {
			enabled: function(val){
				var txtlist = ['<font color=#666>无效<font>', '<font color=#090>私有</font>', '<font color=#00B>内部</font>', '<font color=#E00>公开</font>'];
				return centerText(txtlist[val]);
			},
			maxsz: function(val){
				if (val == null) return centerText('---');
				if (val == 0) val = '无限制';
				else val += 'MB';
				return centerText(val);
			},
			maxcnt: function(val){
				if (val == null) return centerText('---');
				if (val == 0) val = '无限制';
				else val += '次';
				return centerText(val);
			},
			hostmaxcnt: function(val){
				if (val == null) return centerText('---');
				if (val == 0) val = '无限制';
				else val += '次';
				return centerText(val);
			},
			daily: function(val){
				if (val == null) return centerText('---');
				if (val == 0) val = '';
				return centerText(val);
			},
			meancost: function(val){
				if (val == null) return centerText('---');
				function color(val){
					if (val > 1000000) return '<red>' + val + '</red>';
					if (val > 100000) return '<purple>' + val + '</purple>';
					if (val > 50000) return '<blue>' + val + '</blue>';
					return '<green>' + val + '</green>';
				}
				if (val == 0) val = '';
				else val = color(val);
				return centerText(val);
			}
		},
		button: [
			{
				title: '编辑',
				click: editCgiRecord,
				disable: function(item){
					return item.maxsz == null;
				}
			},{
				title: '重置',
				click: resetCgiRecord,
				disable: function(item){
					return item.maxsz == null;
				}
			},{
				title: '接口文档',
				click: showCgidocPage
			}
		]
	};

	var listview = new RecordListView('SystemCgiListDiv', '/getcgilist', vmdata, function(){
		return {path: $('#QueryCgiPathText').val(), enabled: $('#QueryCgiEnabledText').val() - 1, system: getCtrl('QueryCgiSystemText').checked ? 1 : 0, host: getParameter('host'), port: getParameter('port')};
	});

	$('#QueryCgiSystemText').change(function(){
		listview.reload(true);
	});

	$('#QueryCgiConfigButton').click(function(){
		listview.reload(true);
	});

	$('#QueryCgiEnabledText').change(function(){
		listview.reload(true);
	});

	$('#QueryCgiPathText').keyup(function(){
		$(this).val($(this).val().toLowerCase());
		listview.reload(false);
	});

	function editCgiRecord(item){
		showConfirmMessage("<table class='DialogTable' id='CgiEditTable'>"
			+ "<tr><td><v-select id='CgiEnabledText' title='接口类型' option='无效|私有|内部|公开'></v-select></td></tr>"
			+ "<tr><td><v-select id='CgiSizeText' title='请求大小' option='无限制|1MB|2MB|4MB|8MB|16MB|32MB|64MB'></v-select></td></tr>"
			+ "<tr><td><v-select id='CgiCountText' title='并发上限' option='无限制|1次|2次|5次|10次|20次|50次|100次|200次|500次'></v-select></td></tr>"
			+ "<tr><td><v-select id='CgiHostCountText' title='主机限流' option='无限制|10次|20次|50次|100次|200次|500次|1000次|5000次|10000次|50000次'></v-select></td></tr>"
			+ "<tr><td><v-textarea id='CgiRemarkText' title='接口说明' maxlength='128' height='40'></v-textarea></td></tr>"
			+ "</table>", '修改接口配置', function(flag){
			if (flag){
				var maxsz = $('#CgiSizeText').val();
				var maxcnt = $('#CgiCountText').val();
				var remark = $('#CgiRemarkText').val();
				var enabled = $('#CgiEnabledText').val();
				var hostmaxcnt = $('#CgiHostCountText').val();

				hostmaxcnt = hostcntlist[hostmaxcnt];
				maxcnt = cntlist[maxcnt];
				maxsz = szlist[maxsz];

				getHttpResult('/editcgi', {flag: 'U', path: item.path, maxsz: maxsz * 1024 * 1024, maxcnt: maxcnt, remark: remark, enabled: enabled, hostmaxcnt: hostmaxcnt, host: getParameter('host'), port: getParameter('port')}, function(data){
					if (data.code ==  XG_TIMEOUT){
						sessionTimeout();
					}
					else if (data.code < 0){
						showToast('修改接口配置失败');
					}
					else{
						showToast('修改接口配置成功');

						item.maxsz = maxsz;
						item.maxcnt = maxcnt;
						item.remark = remark;
						item.enabled = enabled;
						item.hostmaxcnt = hostmaxcnt;

						listview.update(false);
					}
				});
			}
		});

		$('#CgiRemarkText').val(item.remark);
		$('#CgiEnabledText').val(item.enabled);

		if (item.flag == 0){
			$('#CgiSizeText').val(0).attr('disabled', true);
			$('#CgiCountText').val(0).attr('disabled', true);
			$('#CgiHostCountText').val(0).attr('disabled', true);
		}
		else{
			for (var i = 0; i < szlist.length; i++){
				if (szlist[i] == item.maxsz){
					$('#CgiSizeText').val(i);
					break;
				}
			}
			
			for (var i = 0; i < cntlist.length; i++){
				if (cntlist[i] == item.maxcnt){
					$('#CgiCountText').val(i);
					break;
				}
			}
			
			for (var i = 0; i < hostcntlist.length; i++){
				if (hostcntlist[i] == item.hostmaxcnt){
					$('#CgiHostCountText').val(i);
					break;
				}
			}
		}
	}

	function resetCgiRecord(item){
		showConfirmMessage('重置接口[' + getFileName(item.path) + ']配置？', '重置接口配置', function(flag){
			if (flag){
				getHttpResult('/editcgi', {flag: 'D', path: item.path, host: getParameter('host'), port: getParameter('port')}, function(data){
					if (data.code ==  XG_TIMEOUT){
						sessionTimeout();
					}
					else if (data.code < 0){
						showToast('重置接口配置失败');
					}
					else{
						showToast('重置接口配置成功');
						listview.reload(false);
					}
				});
			}
		});
	}

	function showCgidocPage(item){
		let host = getParameter('host')
		let port = getParameter('port');

		if (strlen(host) > 0){
			window.open('/cgidoc?path=' + item.path + '&host=' + host + '&port=' + port);
		}
		else{
			window.open('/cgidoc?path=' + item.path);
		}
	}
}
</script>